import androidx.compose.runtime.*
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

@Preview
@Composable
fun TimerAppPreview() {
    TimerApp()
}


@Composable
fun TimerApp() {

    val click1:()->Unit = {
        println("click1")
    }

    val click2:()->Unit = {
        println("click2")
    }

    var currentClick by remember {
        mutableStateOf(click1)
    }
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {


        Spacer(modifier = Modifier.height(16.dp))
            Button(onClick = {
                if (currentClick == click1){
                    currentClick = click2}
                else{
                    currentClick = click1
                    }
            }) {
                Text(if (currentClick == click1) "click1" else "click2")
            }


            TimerExample(currentClick)
    }
}


@Composable
fun TimerExample(timeOut: () -> Unit) {

    LaunchedEffect(Unit) {
        println("开始计时")
        repeat(10){
            delay(1000)
            println("倒计时:${it+1}")
        }
        timeOut()
    }
}


